package com.nidu.demo.notify.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.notify.convertor.NotifyTemplateConvertor;
import com.nidu.demo.notify.convertor.NotifyTemplateCriteriaConvertor;
import com.nidu.demo.notify.dto.NotifyTemplateCO;
import com.nidu.demo.notify.dto.NotifyTemplatePageQry;
import com.nidu.demo.notify.gateway.NotifyTemplateGateway;
import com.nidu.demo.notify.model.NotifyTemplate;
import com.nidu.demo.notify.model.NotifyTemplateCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 站内信消息模板分页查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class NotifyTemplatePageQryExe {

    private final NotifyTemplateGateway gateway;
    private final NotifyTemplateConvertor convertor;
    private final NotifyTemplateCriteriaConvertor criteriaConvertor;

    public PageResponse<NotifyTemplateCO> execute(NotifyTemplatePageQry qry) {
        // 1. 转换查询条件
        NotifyTemplateCriteria criteria = criteriaConvertor.toDataObject(qry);

        // 2. 执行分页查询
        PageResponse<NotifyTemplate> page = gateway.pageByCondition(criteria);

        // 3. 转换为DTO并返回
        List<NotifyTemplateCO> notifyTemplateCOS = convertor.toClientObjects(page.getData());
        return PageResponse.of(notifyTemplateCOS, page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }

}
